# coding=utf-8
# 编译日期：2022-09-15 19:48:47
# 版权所有：www.i-search.com.cn
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.base import MIMEBase
from email import encoders
import smtplib
import ubpa.init_input as iinput
import time
import pdb
from ubpa.ilog import ILog
from ubpa.base_img import set_img_res_path
import getopt
from sys import argv
import sys
import os
from base_img import *

class RpaAsiM:
     
    def __init__(self,**kwargs):
        self.__logger = ILog(__file__)
        self.path = set_img_res_path(__file__)
        self.robot_no = ''
        self.proc_no = ''
        self.job_no = ''
        self.input_arg = ''
        if('robot_no' in kwargs.keys()):
            self.robot_no = kwargs['robot_no']
        if('proc_no' in kwargs.keys()):
            self.proc_no = kwargs['proc_no']
        if('job_no' in kwargs.keys()):
            self.job_no = kwargs['job_no']
        if('input_arg' in kwargs.keys()):
            self.input_arg = kwargs['input_arg']
            if(len(self.input_arg) <= 0):
                self.input_arg = iinput.load_init(__file__)
            if self.input_arg is None:
                sys.exit(0)
    def Main(self):
        tmplstDate = datenow.
        mtitle = '{0}城市{1}年{2}月{3}日未来一段时间天气数据'.format(cityname,)
        
        #to_address = 'training@i-search.com.cn'
        to_address = 'goupingping@cmos.chinamobile.com'
        # 填写真实的发邮件服务器用户名、密码
        user = '13483612424@139.com'
        password = 'dxzf@2022'
        # 邮件内容
        
        msg = MIMEMultipart()
        msg.attach(MIMEText(mtitle, 'plain', _charset="utf-8"))
        # 邮件主题描述
        msg["Subject"] = mtitle
        # 发件人显示，不起实际作用
        msg["from"] = '13483612424@139.com'
        # 收件人显示，不起实际作用
        msg["to"] = to_address
        # 抄送人显示，不起实际作用
        msg["Cc"] = ''
        
        att = MIMEText(mtitle, "plain", "utf-8")  # 使用UTF-8编码格式保证多语言的兼容性
        msg.attach(att)
        
        att1 = MIMEBase("application", "octet-stream")
        att1.set_payload(open(new_fpath, "rb").read())
        # 设置编码格式,附件重命名成xxx.docx
        att1.add_header("Content-Disposition", "attachment", filename=Header(new_fpath.split('\\')[-1], "utf-8").encode())
        encoders.encode_base64(att1)
        msg.attach(att1)
        
        # 发送邮件
        smtp = smtplib.SMTP_SSL("smtp.139.com", port=465)
        smtp.login(user=user, password=password)
        smtp.sendmail(user, to_address, msg.as_string())
        smtp.quit()
        
        
        
        #with SMTP_SSL(host='smtp.139.com', port=465) as smtp:
        #    # 登录发邮件服务器
        #    smtp.login(user=user, password=password)
        #    # 实际发送、接收邮件配置
        #    smtp.sendmail(from_addr=user, to_addrs=to_address.split(','), msg=msg.as_string())
 
if __name__ == '__main__':
    ILog.begin_init()
    robot_no = ''
    proc_no = ''
    job_no = ''
    input_arg = ''
    try:
        argv = sys.argv[1:]
        opts, args = getopt.getopt(argv,"hr:p:j:i:",["robot = ","proc = ","job = ","input = "])
    except getopt.GetoptError:
        print ('robot.py -r <robot> -p <proc> -j <job>')
    for opt, arg in opts:
        if opt == '-h':
            print ('robot.py -r <robot> -p <proc> -j <job>')
        elif opt in ("-r", "--robot"):
            robot_no = arg
        elif opt in ("-p", "--proc"):
            proc_no = arg
        elif opt in ("-j", "--job"):
            job_no = arg
        elif opt in ("-i", "--input"):
            input_arg = arg
    pro = RpaAsiM(robot_no=robot_no,proc_no=proc_no,job_no=job_no,input_arg=input_arg)
    pro.Main()
